package cn.mdmm.service.impl.user;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.mdmm.entity.user.UserLoginInfoModel;
import cn.mdmm.entity.user.request.UserRequest;
import cn.mdmm.mapper.user.UserLoginInfoMapper;
import cn.mdmm.service.user.UserLoginInfoService;
import cn.modoumama.common.utils.AddressUtils;
import cn.modoumama.common.utils.StringUtils;
import cn.modoumama.service.base.impl.BaseServiceImpl;


/**
* 类名称：UserTokenInfo对象定义<br>
* 类描述：用户登陆密钥表业务实现类<br>
* 类生产方式：工具自动生成代码<br>
* 创建人：dengqiang<br>
* 创建时间：2017-06-17 15:02:45<br>
* 修改人：  <br>
* 修改时间：<br>
* 修改备注：<br>
* @version   V1.0
*/

@Service
public class UserLoginInfoServiceImpl extends BaseServiceImpl<UserLoginInfoModel,Integer> implements UserLoginInfoService{
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	public void setMapper(UserLoginInfoMapper genericMapper) {
		super.setGenericMapper(genericMapper);
	}

	//------------------请在此添加自定义方法（开始）------------------


	@Override
	public String getToken(UserRequest user) {
		UserLoginInfoModel userLoginInfoModel = new UserLoginInfoModel();
		userLoginInfoModel.setAppId(user.getAppId());
		String ip = user.getIp();
		if(StringUtils.isNotBlank(ip)){
			userLoginInfoModel.setIp(ip);
			try {
				String address = AddressUtils.getAddresses(ip, "UTF-8");
				userLoginInfoModel.setAddress(address);
			} catch (UnsupportedEncodingException e) {
				logger.info("通过IP:{}获取所在地失败",ip);
			}
		}
		
		
		userLoginInfoModel.setLastTime(new Date());
		userLoginInfoModel.setUserId(user.getUserId());
		String token = StringUtils.getToken();
		userLoginInfoModel.setToken(token);
		insert(userLoginInfoModel);
		return token;
	}
	//------------------请在此添加自定义方法（结束）------------------
	
}
